package com.ellricky.baselib.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import com.ellricky.baselib.R
import com.ellricky.baselib.util.StringUtils
import kotlinx.android.synthetic.main.base_layout_data2.view.*

import java.util.*

class DataLayout2 : FrameLayout {

    private var type: Int = TYPE_MONTH
    private var time: Long = 0

    interface OnDataLayoutListener {
        fun onLeftClick()
        fun onRightClick()
    }

    var listener: OnDataLayoutListener? = null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, def: Int) : super(
        context,
        attributeSet,
        def
    ) {
        View.inflate(context, R.layout.base_layout_data2, this)

        val typeArr = context.obtainStyledAttributes(attributeSet, R.styleable.DataLayout2)

        val textOne = typeArr.getString(R.styleable.DataLayout2_data2_text_one) ?: "0"
        val textTwo = typeArr.getString(R.styleable.DataLayout2_data2_text_two) ?: "0"
        val textThree = typeArr.getString(R.styleable.DataLayout2_data2_text_three) ?: "0"
        val textFour = typeArr.getString(R.styleable.DataLayout2_data2_text_four) ?: "0"
        val textFive = typeArr.getString(R.styleable.DataLayout2_data2_text_five) ?: "0"
        val textSix = typeArr.getString(R.styleable.DataLayout2_data2_text_six) ?: "0"
        val textSeven = typeArr.getString(R.styleable.DataLayout2_data2_text_seven) ?: "0"
        val textEight = typeArr.getString(R.styleable.DataLayout2_data2_text_eight) ?: "0"

        val descOne = typeArr.getString(R.styleable.DataLayout2_data2_desc_one) ?: ""
        val descTwo = typeArr.getString(R.styleable.DataLayout2_data2_desc_two) ?: ""
        val descThree = typeArr.getString(R.styleable.DataLayout2_data2_desc_three) ?: ""
        val descFour = typeArr.getString(R.styleable.DataLayout2_data2_desc_four) ?: ""
        val descFive = typeArr.getString(R.styleable.DataLayout2_data2_desc_five) ?: ""
        val descSix = typeArr.getString(R.styleable.DataLayout2_data2_desc_six) ?: ""
        val descSeven = typeArr.getString(R.styleable.DataLayout2_data2_desc_seven) ?: ""
        val descEight = typeArr.getString(R.styleable.DataLayout2_data2_desc_eight) ?: ""
        val isShowCalendar = typeArr.getBoolean(R.styleable.DataLayout2_data2_is_show_calendar, true)
        val isShowCalendarMonth =
            typeArr.getBoolean(R.styleable.DataLayout2_data2_is_show_month, true)

        if (descOne.isNotEmpty()) {
            tvData1.text = textOne
            tvDesc1.text = descOne
        } else {
            tvData1.visibility = View.INVISIBLE
            tvDesc1.visibility = View.INVISIBLE
        }

        if (descTwo.isNotEmpty()) {
            tvData2.text = textTwo
            tvDesc2.text = descTwo
        } else {
            tvData2.visibility = View.INVISIBLE
            tvDesc2.visibility = View.INVISIBLE
        }

        if (descThree.isNotEmpty()) {
            tvData3.text = textThree
            tvDesc3.text = descThree
        } else {
            tvData3.visibility = View.INVISIBLE
            tvDesc3.visibility = View.INVISIBLE
        }

        if (descFour.isNotEmpty()) {
            tvData4.text = textFour
            tvDesc4.text = descFour
        } else {
            tvData4.visibility = View.INVISIBLE
            tvDesc4.visibility = View.INVISIBLE
        }

        if (descFive.isNotEmpty()) {
            tvData5.text = textFive
            tvDesc5.text = descFive
        } else {
            tvData5.visibility = View.INVISIBLE
            tvDesc5.visibility = View.INVISIBLE
        }

        if (descSix.isNotEmpty()) {
            tvData6.text = textSix
            tvDesc6.text = descSix
        } else {
            tvData6.visibility = View.INVISIBLE
            tvDesc6.visibility = View.INVISIBLE
        }

        if (descSeven.isNotEmpty()) {
            tvData7.text = textSeven
            tvDesc7.text = descSeven
        } else {
            tvData7.visibility = View.INVISIBLE
            tvDesc7.visibility = View.INVISIBLE
        }

        if (descEight.isNotEmpty()) {
            tvData8.text = textEight
            tvDesc8.text = descEight
        } else {
            tvData8.visibility = View.INVISIBLE
            tvDesc8.visibility = View.INVISIBLE
        }

        if (isShowCalendar) {
            llDate.visibility = View.VISIBLE
        }

        icLeftArrow.setOnClickListener {
            when (type) {
                TYPE_MONTH -> {
                    time = getPreMonth()
                    tv_month.text = StringUtils.conversionTime(time, "yyyy.MM")
                }
                TYPE_YEAR -> {
                    time = getPreYear()
                    tv_month.text =  StringUtils.conversionTime(time, "yyyy")
                }
            }
            listener?.onLeftClick()
        }

        icRightArrow.setOnClickListener {
            when (type) {
                TYPE_MONTH -> {
                    time = getNextMonth()
                    tv_month.text = StringUtils.conversionTime(time, "yyyy.MM")
                }
                TYPE_YEAR -> {
                    time = getNextYear()
                    tv_month.text =  StringUtils.conversionTime(time, "yyyy")
                }
            }
            listener?.onRightClick()
        }

        typeArr.recycle()
    }

    fun setFirstData(text: String) {
        tvData1.text = text
    }

    fun setSecondData(text: String) {
        tvData2.text = text
    }

    fun setThirdData(text: String) {
        tvData3.text = text
    }

    fun setFourData(text: String) {
        tvData4.text = text
    }

    fun setFiveData(text: String) {
        tvData5.text = text
    }

    fun setSixData(text: String) {
        tvData6.text = text
    }

    fun setSevenData(text: String) {
        tvData7.text = text
    }

    fun setEightData(text: String) {
        tvData8.text = text
    }

    fun setMonthData(time: Long) {
        this.time = time
        when (type) {
            TYPE_MONTH -> {
                val timeString = StringUtils.conversionTime(time, "yyyy.MM")
                tv_month.text = timeString
            }
            TYPE_YEAR -> {
                val timeString = StringUtils.conversionTime(time, "yyyy")
                tv_month.text = timeString
            }
        }
    }

    fun setOnDataLayoutListener(listener: OnDataLayoutListener) {
        this.listener = listener
    }

    fun getCurrentTime(): Long {
        return time
    }

    fun setShowType(type: Int) {
        this.type = type
    }

    private fun getPreMonth(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        calendar.add(Calendar.MONTH, -1)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }

    private fun getNextMonth(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        calendar.add(Calendar.MONTH, +1)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }

    private fun getPreYear(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        calendar.add(Calendar.YEAR, -1)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }

    private fun getNextYear(): Long {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = time
        calendar.add(Calendar.YEAR, +1)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }
}